#include "upgradedialog.h"
#include <QApplication>
#include <QClipboard>
#include <QLabel>
#include <QMimeData>
#include <QMouseEvent>
#include <QPushButton>
#include <QVBoxLayout>

namespace yc {

UpgradeDialog::UpgradeDialog(const QString& app_name, const QString& current_version,
                             const QString& current_build_time, bool has_new_version,
                             const QString& new_version, const QString& date, QWidget* parent)
    : QDialog(parent, Qt::MSWindowsFixedSizeDialogHint), current_version_(current_version)
{
    setWindowTitle(tr("Check Upgrade"));

    QVBoxLayout* main_layout = new QVBoxLayout(this);
    main_layout->setContentsMargins(20, 40, 20, 20);

    QHBoxLayout* hbox_layout = new QHBoxLayout;
    hbox_layout->setContentsMargins(0, 0, 0, 0);
    hbox_layout->setSpacing(20);
    main_layout->addLayout(hbox_layout);
    main_layout->addStretch(1);

    QFrame* icon = new QFrame(this);
    icon->setObjectName("icon");
    hbox_layout->addWidget(icon, 0, Qt::AlignLeft | Qt::AlignVCenter);

    QVBoxLayout* vbox_layout = new QVBoxLayout;
    vbox_layout->setContentsMargins(0, 0, 0, 0);
    vbox_layout->setSpacing(5);
    hbox_layout->addLayout(vbox_layout, 1);

    QLabel* label1 = new QLabel(this);
    label1->setObjectName("label1");
    vbox_layout->addWidget(label1);
    vbox_layout->addSpacing(10);

    QLabel* label2 = new QLabel(this);
    label2->setObjectName("label2");
    vbox_layout->addWidget(label2);

    QLabel* label3 = new QLabel(this);
    label3->setObjectName("label3");
    vbox_layout->addWidget(label3);

    if (has_new_version) {
        label1->setText(tr("New version"));
        label2->setText(tr("%1 %2 %3 Published").arg(app_name, new_version, date));
        label3->setText(tr("Do you upgrade latest %1 %2 version?").arg(new_version, date));

        QHBoxLayout* btn_layout = new QHBoxLayout;
        btn_layout->setContentsMargins(0, 0, 0, 0);
        btn_layout->setSpacing(10);
        btn_layout->addStretch(1);
        main_layout->addLayout(btn_layout);

        QPushButton* btn_update = new QPushButton(tr("Update"), this);
        btn_update->setObjectName("normal");
        btn_update->setDefault(true);
        btn_layout->addWidget(btn_update);

        QPushButton* btn_ignore = new QPushButton(tr("Ignore"), this);
        btn_ignore->setObjectName("normal");
        btn_layout->addWidget(btn_ignore);

        connect(btn_update, &QPushButton::clicked, this, [this] {
            Q_EMIT upgradeRequested();
            close();
        });
        connect(btn_ignore, &QPushButton::clicked, this, [this] {
            Q_EMIT ignoreRequested();
            close();
        });
    } else {
        label1->setText(tr("Latest"));
        label2->setText(tr("Version: ") + current_version);
        label3->setText(tr("Date: ") + current_build_time);
    }
}

} // namespace yc
